var vm = new Vue({ 
    el: '#container' 
    , data: {
        buttons: {}
        , query: {
            page: 1
            , pageSize: hcCore.pageSize
            , typeName: ''
        }
        , tableData: {
            records: []
            , total: 0
        }
        , drawer: {
            visible: false
            , records: []
            , data: []
        }
        , edit: {
            formId: 'editFrame'
            , title: '操作'
            , visible: false
            , fields: [{
                label: '键值'
                , name: 'id'
                , require: true
                , type: 'hidden'
                , disabled: false
                , value: ''
                , placeholder: '请输入键值'
                , rules: {
                    required: true, message: '键值不能为空', trigger: 'blur'
                }
            }, {
                label: '分类名称'
                , name: 'type_name'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入分类名称'
                , rules: {
                    required: true, message: '分类名称不能为空', trigger: 'blur'
                }
            }, {
                label: '分类颜色'
                , name: 'type_color'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入分类名称'
                , rules: {
                    required: true, message: '分类名称不能为空', trigger: 'blur'
                }
            }, {
                label: '分类说明'
                , name: 'type_desc'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入分类说明'
                , rules: {
                    required: true, message: '分类说明不能为空', trigger: 'blur'
                }
            }, {
                label: '分组'
                , name: 'type_group'
                , require: true
                , type: 'text'
                , disabled: false
                , value: ''
                , placeholder: '请输入分组'
                , rules: {
                    required: true, message: '分组不能为空', trigger: 'blur'
                }
            }, {
                label: '分离排序'
                , name: 'type_order'
                , require: true
                , type: 'number'
                , disabled: false
                , value: ''
                , placeholder: '请输入分离排序'
                , rules: {
                    required: true, message: '分离排序不能为空', trigger: 'blur'
                }
            }, {
                label: '分组排序'
                , name: 'type_show_order'
                , require: true
                , type: 'number'
                , disabled: false
                , value: ''
                , placeholder: '请输入分组排序'
                , rules: {
                    required: true, message: '分组排序不能为空', trigger: 'blur'
                }
            }]
            , op: function () {
            }
        }
    }
    , mounted: async function () { 
        const me = this; 
        me.buttons = await hcCore.getPowerButton(); 
        if (me.buttons.hasQuery) { 
            me.onQuery(); 
            me.getConditionType();
        } 
    } 
    , methods: { 
        handleButton: function (act_no, row) { 
            switch (act_no) { 
                case 'Add': 
                    this.showEdit('add', '新增'); 
                    break; 
                case 'Edit': 
                    this.showEdit('edit', '编辑', row); 
                    break; 
                case 'Delete': 
                    this.onDelete(row); 
                    break; 
                default: 
                    break; 
            } 
        } 
        , onQuery: function () { 
            let me = this; 
            hcCore.post({ 
                url: '/MacRuntype/List' 
                , param: { 
                    page: me.query.page 
                    , page_size: me.query.pageSize 
                    , type_name: me.query.typeName 
                } 
                , success: function (rsp) { 
                    me.tableData.records = rsp.data; 
                    me.tableData.total = rsp.total; 
                } 
            }) 
        } 
        , onPageChange: function (page) { 
            this.query.page = page; 
            this.onQuery(); 
        } 
        , showEdit: async function (type, title, row) { 
            let me = this; 
            me.resetParams(me.edit.fields); 
            me.edit.title = title; 
            me.edit.op = type == 'add' ? me.onInsert : (type == 'edit' ? me.onUpdate : function () { }); 
            if (type == 'edit') { 
                if (row) { 
                    await hcCore.post({ 
                        url: '/MacRuntype/Get/' + row.id 
                        , success: function (rsp) { 
                            me.edit.fields.forEach((item, index) => { 
                                if (rsp.data[item.name]) { 
                                    item.value = rsp.data[item.name]; 
                                } 
 
                                if (item.name == 'id') { 
                                    item.disabled = true; 
                                } 
                            }); 
                        } 
                        , fault: '获得数据失败' 
                    }); 
                } 
                else { 
                    hcCore.showError('请选择要编辑的记录'); 
                    return; 
                } 
            } else { 
 
            } 
            me.edit.visible = true; 
        } 
        , onInsert: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/MacRuntype/Insert' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('新增成功'); 
                            me.tableData.records.push(rsp.data); 
                            me.tableData.total += 1; 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onUpdate: function () { 
            let me = this; 
            me.$refs.edit.$refs[me.edit.formId].validate((valid) => { 
                if (valid) { 
                    hcCore.post({ 
                        url: '/MacRuntype/Update' 
                        , param: me.getParams() 
                        , success: function (rsp) { 
                            hcCore.showSuccess('更新成功'); 
                            let index = me.tableData.records.findIndex(item => item.id == rsp.data.id); 
                            me.$set(me.tableData.records, index, rsp.data) 
                            me.edit.visible = false; 
                        } 
                    }); 
                } else { 
                    return false; 
                } 
            }); 
        } 
        , onDelete: function (row) { 
            let me = this; 
            let rows = []; 
            if (row) { 
                rows = [row]; 
            } 
            else { 
                rows = me.$refs.tblRecords.selection; 
            } 
            if (rows && rows.length > 0) { 
                let tips = rows.length == 1 ? ('确认要删除【'+ rows[0].id +'】这条记录吗') : ('确认要删除选择的【' + rows.length + '】条的记录吗？') 
                hcCore.confirm(tips, function () { 
                    let arr = rows.map(item => { return item.id }); 
                    let nos = arr.join(','); 
                    hcCore.post({ 
                        url: '/MacRuntype/Delete' 
                        , param: { 
                            ids: nos 
                        } 
                        , success: function (rsp) { 
                            hcCore.showSuccess('删除成功'); 
                            arr.forEach(item => { 
                                let index = me.tableData.records.findIndex(node => item == node.id); 
                                me.tableData.records.splice(index, 1); 
                            }); 
                        } 
                    }); 
                }); 
            } 
            else { 
                hcCore.showError('请选择要删除的记录'); 
            } 
        } 
        , onExport: function () { 
            let paramArr = []; 
            paramArr.push('type_name='+this.query.typeName) 
             
            hcCore.handleExport('/MacRuntype/Export?'+paramArr.join('&')); 
        } 
        , getParams: function () { 
            let param = {}; 
            this.edit.fields.forEach((item, index) => { 
                param[item.name] = item.value; 
            }); 
            return param; 
        } 
        , resetParams: function (obj) { 
            obj.forEach(item => { 
                item.value = ''; 
                item.disabled = false; 
            }); 
        } 
        , getConditionType: function () {
            let me = this;
            hcCore.post({
                url: '/MacRuntype/ListConditionType'
                , param: {
                }
                , success: function (rsp) {
                    me.drawer.records = rsp.data;
                    for (let i = 0; i < me.drawer.records.length; i++) {
                        me.$set(me.drawer.records[i], 'value', []);
                        me.$set(me.drawer.records[i], 'rel', -1);
                    }
                }
            }) 
        }
        , recordIndex: function (index) { 
            return (this.query.page - 1) * this.query.pageSize + index + 1; 
        } 
        , showCondition: function (row, column, event) {
            let me = this;
            this.drawer.visible = true;
            this.drawer.selectedTypeID = row.id;
            hcCore.post({
                url: '/MacRuntype/GetConditionInfo'
                , param: {
                    id: row.id
                }
                , success: function (rsp) {
                    for (let i = 0; i < me.drawer.records.length; i++) {
                        let item = rsp.data.find(function (x) {
                            return x.condition_type_id == me.drawer.records[i].id;
                        });
                        if (item) {
                            me.drawer.records[i].value = [item.condition_value];
                            me.drawer.records[i].rel = item.condition_rel;
                        }
                        else {
                            me.drawer.records[i].value = [];
                            me.drawer.records[i].rel = -1;
                        }
                    }
                }
            }) 
        }
        , setConditionSelect: function (item) {
            item.value = item.value.slice(-1);
        }
        , commitCondition: function () {
            let me = this;
            let param = me.drawer.records.filter(function (x) {
                return x.value && x.value.length > 0;
            }).map(function (x) {
                return {
                    condition_type_id: x.id
                    , condition_value: x.value[0]
                };
            });
            if (param.length == 0) {
                hcCore.showError('请选择识别条件');
                return;
            }
            console.log(param);
            hcCore.post({
                url: '/MacRuntype/CommitConditionInfo'
                , param: {
                    typeid: me.drawer.selectedTypeID
                    , condition: JSON.stringify(param)
                }
                , success: function (rsp) {
                    let index = me.tableData.records.findIndex(item => item.id == rsp.data.id);
                    me.$set(me.tableData.records, index, rsp.data);
                    hcCore.showSuccess('提交成功');
                }
            }) 
        }
    } 
});